package com.fan.fan.receiver;

import java.util.Calendar;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class AlarmReceiver extends BroadcastReceiver {
	private MediaPlayer mp;
	Context context;

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		int minute = calendar.get(Calendar.MINUTE);
		CharSequence cs = String.valueOf(minute);
		this.context = context;
//		Toast.makeText(context, "charSequence ="+cs, Toast.LENGTH_LONG).show();
		Bundle bundle = intent.getExtras();
		Intent serviceIntent = new Intent("main_service");
		serviceIntent.putExtras(bundle);
		if (bundle != null) {
			Log.i("1", String.valueOf(bundle.getBoolean("isPlayMusic")));
			if (bundle.getBoolean("isPlayMusic"))
				context.startService(serviceIntent);
			else
				context.stopService(serviceIntent);
		}
	}

	private void ringAlarmTune() {
		Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
		try {
			mp = new MediaPlayer();
			mp.setDataSource(context, uri);
			final AudioManager am = (AudioManager) context
					.getSystemService(Context.AUDIO_SERVICE);
			if (am.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
				mp.setAudioStreamType(AudioManager.STREAM_ALARM);
				mp.setLooping(true);
				mp.prepare();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		mp.start();
	}

	private void StopAlarmRing() {
		mp.stop();
	}
}
